import { mailLogMock } from './data/log'
import { isDateTimeInRange } from '../../../utils'
let mailLogList = mailLogMock(400)
export default [
  {
    url: RegExp('/system/mail-log/detail' + '.*'), // 获取详情
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getInfoById(params)
      }
    }
  },
  {
    url: RegExp('/system/mail-log/page' + '.*'), // 查询分页列表
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByPage(params)
      }
    }
  }
]

const getListByPage = params => {
  let result = mailLogList
  result = result.filter(item => !item.deleted)
  if (params.query.userId) {
    result = result.filter(item => item.userId === parseInt(params.query.userId))
  }
  if (params.query.userType) {
    result = result.filter(item => item.userType === parseInt(params.query.userType))
  }
  if (params.query.sendStatus) {
    result = result.filter(item => item.sendStatus === parseInt(params.query.sendStatus))
  }
  if (params.query.accountId) {
    result = result.filter(item => item.accountId === parseInt(params.query.accountId))
  }
  if (params.query.templateId) {
    result = result.filter(item => item.templateId === parseInt(params.query.templateId))
  }
  if (params.query.toMail) {
    result = result.filter(item => item.toMail.indexOf(params.query.toMail) !== -1)
  }
  if (params.query.sendTime && params.query.sendTime.length !== 0) {
    result = result.filter(item => isDateTimeInRange(params.query.sendTime[0], params.query.sendTime[1], item.sendTime))
  }
  return {
    list: result.slice(parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1), parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1) + parseInt(params.query.pageSize)),
    total: result.length
  }
}

const getInfoById = params => {
  const result = mailLogList.filter(item => {
    return item.id === parseInt(params.query.id)
  })
  if (result.length > 0) {
    return result[0]
  } else {
    return null
  }
}
